<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use \GatewayWorker\Lib\Gateway;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;

use  App\Models\CallRecord;
use  App\Models\Phone;

class CallRecordController extends Controller
{
    public function list(Request $request)
    {
        $input = Input::only('page','limit','start_at','end_at','str','call_type','hold_time_start_at','hold_time_end_at');
        $validator = Validator::make($input, [        
            'start_at' => 'nullable',
            'end_at' => 'nullable',
            'str' => 'nullable',
            'call_type' => 'nullable',
            'hold_time_start_at' => 'nullable',
            'hold_time_end_at' => 'nullable',
            'page' => 'required',
            'limit' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        // // msg_type
        $record = CallRecord::leftJoin('t_phone', 't_phone.imei', '=', 't_call_record.imei')->leftJoin('t_user', 't_user.id', '=', 't_phone.user_id');
        if(isset($input['start_at'])&&isset($input['end_at'])){
            $record->whereBetween('started_at',[$input['start_at'],$input['end_at']]);
        }
        if(isset($input['str'])){
            $record->where('t_user.name','like' , '%'.$input['str'].'%')->orwhere('t_phone.imei','like' , '%'.$input['str'].'%');
        }
        if(isset($input['call_type'])){
            $record->where('call_type',$input['call_type']);
        }
        if(isset($input['hold_time_start_at'])&&isset($input['hold_time_end_at'])){
            $record->whereBetween('hold_time',[$input['hold_time_start_at'],$input['hold_time_end_at']]);
        }
        $page = $input['page'];
        $limit = $input['limit'];
        $count =  $record->count();
        $data = $record
            ->select('t_call_record.id','t_call_record.call_type','t_call_record.imei','t_call_record.called_number','t_call_record.started_at','t_call_record.hold_time','t_call_record.end_at','t_call_record.file_url','t_user.name as phone_owner')
            ->offset(($page-1)*$limit)
            ->orderBy('t_call_record.id','desc')
            ->limit($limit)
            ->get();
        foreach ($data as $key => $value) {
            $file = json_decode($value['file_url']);
            if($file&&is_object($file)){
                $data[$key]['file_url'] = $file->data->file_url;
            }
            if($value['end_at']){
                $data[$key]['hold_time'] = $value['end_at']/1000;
            }
        }
        $fin['count'] = $count;
        $fin['data'] = $data;
        return $this->returnOk($fin);
    }

    public function delete(Request $request)
    {
        $input = Input::only('id');
        $validator = Validator::make($input, [   
            'id'      => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
       
        $where['id'] = $input['id'];
        $exist = CallRecord::where($where)->first();
        if($exist){
            $exist->delete();
        }else{
            return $this->returnError([],409,'记录不存在');
        }
        return $this->returnOk();
    }
}